﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Sitecore.Data.Items;
using Sitecore.ChangesTracker.Events;
using Sitecore.Data;
using Sitecore.Diagnostics;
using Sitecore.Data.Archiving;
using Sitecore.ChangesTracker.Caching;
using Sitecore.ChangesTracker.Configuration;
using Sitecore.Data.Managers;
using Sitecore.Data.Templates;

namespace Sitecore.ChangesTracker.Archiving
{
    class TrackerManager
    {
        private static LimitedDictionary itemsToSkip = new LimitedDictionary();

        private static TemplateHashCache templateHashCache;

        public static TemplateHashCache TemplateHashCache
        {
            get 
            {
                if (templateHashCache == null)
                {
                    templateHashCache = new TemplateHashCache("TemplateHashCache",Settings.TemplateHashCacheMaxSize);
                }
                return TrackerManager.templateHashCache; 
            }
            
        }

        public static void SkipItem(Item item)
        {           
            SkipItem(item.ID.ToString());
        }
        public static void SkipItem(string Id)
        {
            itemsToSkip.Add(Id, DateTime.Now);
        }

        public static void ArchiveItem(Item item, string info, out Guid archivalId)
        {
            archivalId = Guid.Empty;
            if (ShouldBeArchived(item))
            {
                item = TemplateTracker.GetItemToArchive(item,info);
               
                    if (item != null)
                    {
                        DoArchive(item, info, out archivalId);
                    }
                
            }
        }

        public static void ArchiveItem(Item item, string info)
        {
            Guid archivalId = Guid.Empty;
            if (ShouldBeArchived(item))
            {
                item = TemplateTracker.GetItemToArchive(item,info);
                if (item != null)
                {
               
                    
                        DoArchive(item, info, out archivalId);
                    }
                
            }
        }

        

        private static void DoArchive(Item item, string info, out Guid archivalId)
        {
            Database database = item.Database;
            archivalId = Guid.Empty;
            Assert.IsNotNull(database, typeof(Database), "Name: {0}", new object[]
        {
          item.Database.Name
        });

            ChangeTrackerArchive archive = ArchiveManager.Providers["changetracker"].GetArchive("changetracker", database) as ChangeTrackerArchive;
            if (archive != null)
            {
                Log.Audit(typeof(TrackerManager), "Archive item: {0}", new string[]
          {
            AuditFormatter.FormatItem(item)
          });

                archivalId = archive.ArchiveItem(item, info);
            }
        }

        private static bool ShouldBeArchived(Item item)
        {
            if (ArchiveDisabler.IsActiveForItem(item.ID.ToGuid()))
            {
                return false;
            }
            return !itemsToSkip.ContainsKey(item.ID.ToString());
        }
    }
}
